<?php

namespace App\Domain\FamilyMember\Services;

use App\Domain\FamilyMember\Dao\FamilyMemberDao;
use App\Domain\FamilyMember\Models\FamilyMemberModel;
use App\Exceptions\BadRequestException;
use Illuminate\Database\Eloquent\Builder;

class FamilyMemberService
{
    public function updatemember($param)
    {
        $menberDao = new FamilyMemberDao();
        if ($param['id']) {
            $data = $menberDao->findById($param['id']);
            if (empty($data)) {
                throw new BadRequestException('成员不存在');
            }
            $res = [
                'id' => $param['id'],
                'name' => $param['name'],
                'gender' => $param['gender'],
                'age' => $param['age'],
                'idCard' => $param['idCard'],
                'phone' => $param['phone'],
                'height' => $param['height'],
                'weight' => $param['weight'],
                'relation' => $param['relation'],
                'houseHolder_id' => $param['houseHolder_id'],
            ];
            $data = $menberDao->update($data, $res);
            return $data['id'];
        } else {
            $data = $menberDao->findByPhone($param['phone']);
            if ($data) {
                throw new BadRequestException('手机号已存在');
            }
            $res = $menberDao->create([
                'id' => create_uuid(),
                'name' => $param['name'],
                'gender' => $param['gender'],
                'age' => $param['age'],
                'idCard' => $param['idCard'],
                'phone' => $param['phone'],
                'height' => $param['height'],
                'weight' => $param['weight'],
                'relation' => $param['relation'],
                'houseHolder_id' => $param['houseHolder_id'],
            ]);
            return $res;
        }
    }

    public function list($houseHolder_id)
    {
        $familyDao = new FamilyMemberDao();
        $list = $familyDao->findAll($houseHolder_id);
        return $list;
    }

    public function getInfo($houseHolder_id)
    {
        $familyDao = new FamilyMemberDao();
        $data = $familyDao->findById($houseHolder_id);
        return $data;
    }

    public function getByName($name)
    {
        $familyDao = new FamilyMemberDao();
        $data = $familyDao->findByName($name);
        return $data;
    }

    public function getByIdcard($idcard)
    {
        $familyDao = new FamilyMemberDao();
        $data = $familyDao->findByIdcard($idcard);
        return $data;
    }

    public function getByNameAndPhoneAndIdcard($param)
    {
        $query = FamilyMemberModel::query()->select('family_member.*')->where('state', '1');
        $query = $query->where(function (Builder $query) use ($param) {
            if (isset($param['name']))
                $query->where('name', $param['name']);
            if (isset($param['phone']))
                $query->where('phone', $param['phone']);
            if (isset($param['idcard']))
                $query->where('idcard', $param['idcard']);
        });
        return $query->get();
    }
}
